<script setup>
import { ref, onMounted } from 'vue'
import { getCheckItemPage, deleteCheckItems } from '@/api/checkItem'
import { ElMessage, ElMessageBox } from 'element-plus'

const checkItemList = ref([])
const total = ref(0)
const loading = ref(false) 
const emits = defineEmits(['update', 'add'])

const pageParams = ref({
  pageNum: 1,
  pageSize: 10,
  name: ''
})

const queryList = async() => {
  if (loading.value) return 
  loading.value = true
  try {
    const result = await getCheckItemPage(pageParams.value)
    if (result && result.code === 200) { 
      checkItemList.value = result.data?.records || []
      total.value = result.data?.total || 0
    } else {
      ElMessage.error('接口返回格式错误')
    }
  } catch (error) {
    console.error('列表查询失败：', error)
    ElMessage.error('获取列表失败，请检查网络或接口')
  } finally {
    loading.value = false
  }
}

const handleEdit = (row) => {
  emits('update', row)
}

const handleAdd = () => {
  emits('add')
}

const handleDelete = async(id) => {
  try {
    await ElMessageBox.confirm('确定要删除该检验项目吗？', '警告', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await deleteCheckItems([id])
    ElMessage.success('删除成功')
    queryList()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败：' + (error.message || '网络错误'))
    }
  }
}

const handleSearch = () => {
  pageParams.value.pageNum = 1
  queryList()
}

const handlePageChange = (pageNum, pageSize) => {
  pageParams.value.pageNum = pageNum
  pageParams.value.pageSize = pageSize
  queryList()
}

defineExpose({ queryList })

onMounted(() => {
  queryList()
})
</script>

<template>
  <div class="check-item-list">
    <div class="operation-bar" style="margin-bottom: 16px; display: flex; justify-content: space-between;">
      <el-input
        v-model="pageParams.name"
        placeholder="请输入名称搜索"
        style="width: 300px;"
        clearable
        @keyup.enter="handleSearch"
      />
      <el-button type="primary" @click="handleAdd">新增检验项目</el-button>
    </div>

    <el-table 
      :data="checkItemList" 
      style="width: 100%" 
      border
      v-loading="loading"
      element-loading-text="加载中..."
    >
      <el-table-column label="序号" width="60">
        <template #default="scope">
          {{ (pageParams.pageNum - 1) * pageParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>      
      <el-table-column label="名称" prop="name" />
      <el-table-column label="检验类型" prop="checkTypeId" />
      <el-table-column label="作业方式" prop="method" />
      <el-table-column label="标准值" prop="standardValue" />
      <el-table-column label="单位" prop="unit" />
      <el-table-column label="上限值" prop="max" />
      <el-table-column label="下限值" prop="min" />
      <el-table-column label="备注" prop="description" />
      <el-table-column label="创建人" prop="createBy" />
      <el-table-column label="创建时间" prop="createTime" />
      <el-table-column label="更新时间" prop="updateTime" />
      <el-table-column label="操作" align="right" width="200"  fixed="right">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-top: 16px; text-align: right;"
      :current-page="pageParams.pageNum"
      :page-size="pageParams.pageSize"
      :total="total"
      @current-change="handlePageChange"
      @size-change="handlePageChange"
      layout="total, sizes, prev, pager, next, jumper"
    />
  </div>
</template>